package practica2;

public class CuaCircular extends CuaImpl {

	private Object[] array;
	private int index;

	public CuaCircular(int size){
		capacitat = size;
		array = new Object[capacitat];
	}

	public Object get() {
		Object t = array[index];
		array[index] = null;
		index = (index+1)%capacitat;
		num_elements--;
		return t;
	}

	public void put(Object valor) {
		array[(index+num_elements)%capacitat] = valor;
		num_elements++;
	}

	public static void main(String[] args){
		System.out.println("Prova de la cua circular:");

		CuaCircular cc = new CuaCircular(5);
		System.out.println(cc.toString());
		cc.put("Primer element");
		cc.put("n2 element");
		cc.put("3 element");
		System.out.println(cc.toString());
		cc.get();
		cc.get();
		System.out.println(cc.toString());
		cc.put("4t element");
		cc.put("5e element");
		System.out.println(cc.toString());
		cc.put("6e element");
		System.out.println(cc.toString());
		cc.get();
		System.out.println(cc.toString());
		cc.put("7e element");
		cc.put("8e element");
		System.out.println(cc.toString());
		cc.get();
		cc.get();
		System.out.println(cc.toString());
		cc.get();
		cc.get();
		System.out.println(cc.toString());
		cc.get();
		System.out.println(cc.toString());
	}

	public String toString(){
		String t = "";
		for(int i =0; i < capacitat; i++){
			t += "array["+i+"] = "+(String)array[i]+"\n";
		}
		return t;
	}

}
